package com.swz.dcrm.ui.approval;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.Approval;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Edit;
import com.swz.dcrm.model.Input;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.http.Body;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.ApprovalViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class ApprovalOpinionFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_customer_name)
    TextView tvCustomerName;
    @BindView(R.id.tv_car_model)
    TextView tvCarModel;
    @BindView(R.id.tv_approval_date)
    TextView tvDate;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.rg_approval)
    RadioGroup rgApproval;
    @BindView(R.id.et_remark)
    EditText etRemark;

    boolean pass=true;
    @Inject
    ApprovalViewModel approvalViewModel;
    @Inject
    MainViewModel mainViewModel;
    Approval approval;
    public static ApprovalOpinionFragment newInstance() {

        return new ApprovalOpinionFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }
    @Override
    public int layoutId() {
        return R.layout.fragment_approval_opinion;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.approval_approval));
        tvRight.setText(getString(R.string.confirm));
        mainViewModel.getInput().setValue(new Input(etRemark,0,true ));
        approval=new Gson().fromJson(ApprovalOpinionFragmentArgs.fromBundle(getArguments()).getApproval(),Approval.class);
        tvDesc.setText(getString(R.string.approval_desc,approval.getCounselorName(),""));
        tvCustomerName.setText(approval.getCustomerName());
        tvCarModel.setText(approval.getCarModelName());
        tvDate.setText(getString(R.string.approval_time,approval.getCreateTime()));
        rgApproval.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId){
                case R.id.rb_pass:
                    pass=true;
                    break;
                case R.id.rb_return:
                    pass=false;
                    break;
            }
        });
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }

    @OnClick(R.id.tv_right)
    public void confirm(){
        if(TextUtils.isEmpty(etRemark.getText().toString())){
            showToast(getString(R.string.add_customer_hint)+getString(R.string.approval_opinion));
            return;
        }
        List<Long>longs=new ArrayList<>();
        longs.add(approval.getId());
        approvalViewModel.confirmApprovalOpinion(longs,pass,etRemark.getText().toString())
                .observe(this, new Observer<BaseResponse<String>>() {
                    @Override
                    public void onChanged(BaseResponse<String> stringBaseResponse) {
                        showToast(stringBaseResponse.getMsg());
                        if(stringBaseResponse.isSuccess()){
                            backClick();
                        }
                    }
                });
    }

}
